<?php
/*
 * Created on
 * Date: 2007-12-4
 * Author: William Yang
 */
require_once(dirname(__FILE__)."/../common/constants.php");
require_once(DB_SESSION_DIR_PATH."/db_operator.php");
require_once(DB_SESSION_DIR_PATH."/db_obj_defs.php");

class MemberJoinRequestManager extends MemberJoinRequest {
	public function register() {
		$this->status = MemberJoinRequestStat::REQ_SENT;
		
		$stat = parent::register(CONFIGURATION_DIR_PATH."/create_member_join_request.xml",
								FALSE);
		return (!DbError::is_error($stat));
	}
	
	public static function respond($team_uid, $member_uid, $status) {
		if ($status == MemberJoinRequestStat::REQ_REJECTED) {
			$operator = new DatabaseOperator();
			$operator->config(CONFIGURATION_DIR_PATH."/member_join_request_table.xml");
			
			$delegate = new MemberJoinRequest();
			$delegate->status = $status;
			$info = $delegate->toArray();
			
			$stat = DbError::NO_DB_OPERATION_NEEDED;
			if (count($info)) {
				$condList = new SqlCondList();
				$condList->add_condition(new SqlCondition('team_uid', SqlCondition::EQUAL, $team_uid));
				$condList->add_relation(SqlCondition::_AND);
				$condList->add_condition(new SqlCondition('member_uid', SqlCondition::EQUAL, $member_uid));
				$stat = $operator->update($info, $condList);
			}
			
			return (!DbError::is_error($stat));
		} else if ($status == MemberJoinRequestStat::REQ_ACCEPTED || $status == MemberJoinRequestStat::REQ_CANCELED) {
			$operator = new DatabaseOperator();
			$operator->config(CONFIGURATION_DIR_PATH."/member_join_request_table.xml");
						
			$stat = DbError::NO_DB_OPERATION_NEEDED;
			$condList = new SqlCondList();
			$condList->add_condition(new SqlCondition('team_uid', SqlCondition::EQUAL, $team_uid));
			$condList->add_relation(SqlCondition::_AND);
			$condList->add_condition(new SqlCondition('member_uid', SqlCondition::EQUAL, $member_uid));
			$stat = $operator->delete($condList);
			
			return (!DbError::is_error($stat));
		} else
			return true;
	}
	public static function getReqCntByTeamUID($team_uid) {
		$opera = new DatabaseOperator();
		$opera->config(CONFIGURATION_DIR_PATH."/member_join_request_table.xml");
		
		$cList = new SqlCondList();
		$cList->add_condition(new SqlCondition("team_uid",
											   SqlCondition::EQUAL,
											   $team_uid));
		$info[] = "count(*)";
		$ret = $opera->select($cList, $info);
		$row = $ret[0];
		return $row["count(*)"];
	}
	
	public static function getReqCntByMbrUID($member_uid) {
		$opera = new DatabaseOperator();
		$opera->config(CONFIGURATION_DIR_PATH."/member_join_request_table.xml");
		
		$cList = new SqlCondList();
		$cList->add_condition(new SqlCondition("member_uid",
											   SqlCondition::EQUAL,
											   $member_uid));
		$info[] = "count(*)";
		$ret = $opera->select($cList, $info);
		$row = $ret[0];
		return $row["count(*)"];
	}
	
	public static function getRequestByTeamUID($team_uid) {
		$opera = new DatabaseOperator();
		$opera->config(CONFIGURATION_DIR_PATH."/member_join_request_table.xml");
		
		$cList = new SqlCondList();
		$cList->add_condition(new SqlCondition("team_uid",
											   SqlCondition::EQUAL,
											   $team_uid));
		$ret = $opera->select($cList);
		
		$delegate = new MemberJoinRequest();
		$return_val = array();
		
		if ($ret != NULL) {
			foreach ($ret as $item) {
				$return_val[] = $delegate->array2copy($item);
			}
		}
		
		return $return_val;
	}
	
	public static function getRequestByMbrUID($member_uid) {
		$opera = new DatabaseOperator();
		$opera->config(CONFIGURATION_DIR_PATH."/member_join_request_table.xml");
		
		$cList = new SqlCondList();
		$cList->add_condition(new SqlCondition("member_uid",
											   SqlCondition::EQUAL,
											   $member_uid));
		$ret = $opera->select($cList);
				
		$delegate = new MemberJoinRequest();
		$return_val = array();
		
		if ($ret != NULL) {
			foreach ($ret as $item) {
				$return_val[] = $delegate->array2copy($item);
			}
		}
		
		return $return_val;
	}
	
	public static function isReqSent($team_uid, $member_uid) {
		$opera = new DatabaseOperator();
		$opera->config(CONFIGURATION_DIR_PATH."/member_join_request_table.xml");
		
		$cList = new SqlCondList();
		$cList->add_condition(new SqlCondition("member_uid",
											   SqlCondition::EQUAL,
											   $member_uid));
		$cList->add_relation(SqlCondition::_AND);
		$cList->add_condition(new SqlCondition("team_uid",
											   SqlCondition::EQUAL,
											   $team_uid));
		$ret = $opera->select($cList);
			
		if (count($ret) != 0) {
			return true;
		} else {
			return false;
		}
	}
}
?>
